package com.cloudclassroom.auth;

import com.cloudclassroom.dto.LoginDto;
import com.cloudclassroom.util.JwtUtils;
import com.cloudclassroom.vo.RestResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

/**
 * @author 92110
 */
@RestController
@RequestMapping("/auth")
@Tag(name = "登录模块")
public class LoginController {

    @Resource
    private AdminLoginServiceImpl adminLoginService;
    @Resource
    private CustomerLoginServiceImpl customerLoginService;
    @PostMapping("/admin/login")
    @ResponseStatus(HttpStatus.OK)
    @Operation(tags = "管理员登录", summary = "Login", description = "用于管理员登录的接口")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    public RestResult<String> adminLogin(@RequestBody LoginDto loginDto) {
        UserDetails userDetails = adminLoginService.loadUserByUsername(loginDto.getUsername());
        return RestResult.success(JwtUtils.createJwt((User) userDetails));
    }

    @PostMapping("/customer/login")
    @ResponseStatus(HttpStatus.OK)
    @Operation(summary = "Login", description = "用于用户登录的接口")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    public RestResult<String> customerLogin(@RequestBody LoginDto loginDto) {
        UserDetails userDetails = customerLoginService.loadUserByUsername(loginDto.getUsername());
        return RestResult.success(JwtUtils.createJwt((User) userDetails));
    }
}
